package parser;

import host.impl.Host;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import manager.impl.ServiceManager;

import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import properties.PropertyManager;



public class HostFileParser {
	
	private static Document document;
	private static Element racine=null;
	
	private static String inputFileName;
	private static String outputFileName;
	
	public void setInputFileName(String inputFileName) {
		HostFileParser.inputFileName = inputFileName;
	}

	public void setOutPutFileName(String outputFileName) {
		HostFileParser.outputFileName = outputFileName;
	}

	public Element getRacine(){
		return racine;
	}

	public HostFileParser() {

	}
	
	public void initParser(){
		outputFileName = inputFileName;
		openXMLFile();
	}

	public static void openXMLFile(){
		
		SAXBuilder sxb = new SAXBuilder();
		try{
			document = sxb.build(new File(inputFileName));
		}
		catch(Exception e){
			ServiceManager.printLog(PropertyManager.properties.getProperty("fileOpenningError")+" "+inputFileName);
			e.printStackTrace();
		}
		
		racine = document.getRootElement();
	}
	
	@SuppressWarnings("rawtypes")
	public static ArrayList<Host> extractXMLHostsInObjects(){
		
		ArrayList<Host> hosts = new ArrayList<Host>();
		
		List xmHosts = racine.getChildren("host");
		Iterator i = xmHosts.iterator();
		
		while(i.hasNext())
		{
			Element courant = (Element)i.next();
			
			Host host = new Host(courant.getChild("ipAddress").getText(),
									Integer.parseInt(courant.getChild("registryPortNumber").getText()),
									Integer.parseInt(courant.getChild("sshPortNumber").getText()),
									courant.getChild("rtdgPath").getText(),
									courant.getChild("contextFile").getText(),
									courant.getChild("login").getText(),
									courant.getChild("pwd").getText());
			hosts.add(host);
		}
		ServiceManager.printLog(PropertyManager.properties.getProperty("extractionEnd")+" "+hosts.size());
		return hosts;
	}
	
	public static void setHostInXmlFile(Host host){
		Element toAdd = new Element("host");
		
		  Element ipAddress = new Element("ipAddress");
		  ipAddress.setText(host.getIpAddress());
	      toAdd.addContent(ipAddress);
	      
	      Element registryPortNumber = new Element("registryPortNumber");
	      registryPortNumber.setText(new Integer(host.getRegistryPortNumber()).toString());
	      toAdd.addContent(registryPortNumber);
	      
	      Element sshPortNumber = new Element("sshPortNumber");
	      sshPortNumber.setText(new Integer(host.getSshpPortNumber()).toString());
	      toAdd.addContent(sshPortNumber);
	      
	      Element rtdgPath = new Element("rtdgPath");
	      rtdgPath.setText(host.getRtdgPath());
	      toAdd.addContent(rtdgPath);
	      
	      Element contextFile = new Element("contextFile");
	      contextFile.setText(host.getContextFile());
	      toAdd.addContent(contextFile);
	      
	      Element login = new Element("login");
	      login.setText(host.getLogin());
	      toAdd.addContent(login);
	      
	      Element pwd = new Element("pwd");
	      pwd.setText(host.getPwd());
	      toAdd.addContent(pwd);
	      
	      racine.addContent(toAdd);
	      save();
	}
	
	public static void removeHostInXmlFile(int index){
		ServiceManager.printLog(PropertyManager.properties.getProperty("removingHost")+" "+racine.getContent(index).getValue());
	    racine.removeContent(racine.getContent(index));
	    save();
	}
	
	private static void save()
	{
	   try
	   {
	      XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());

	      sortie.output(document, new FileOutputStream(outputFileName));
	   }
	   catch (java.io.IOException e){
		   ServiceManager.printLog(PropertyManager.properties.getProperty("errorOnSaving"));
	   }
	}
}
